//
//  EatViewController.m
//  Delicious2
//
//  Created by FSD on 16/1/7.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "EatViewController.h"
#import "EatTableViewCell.h"
#import "EatModel.h"
#import "EatShowViewController.h"
#import "MJChiBaoZiFooter.h"
#import "MJChiBaoZiHeader.h"


@interface EatViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *eatDataSource;
@property (nonatomic, assign) NSInteger num;

@property (nonatomic, strong) MBProgressHUD *HUD;

@end

@implementation EatViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"吃天下";
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[EatTableViewCell class] forCellReuseIdentifier:@"eatTableViewIdentifer"];
    
    [self.view addSubview:self.tableView];
    
    [self getEatNetWork];
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载";
    
    self.tableView.mj_footer = [MJChiBaoZiFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadingData)];
    [self.tableView.mj_footer beginRefreshing];
    
    self.tableView.mj_header = [MJChiBaoZiHeader headerWithRefreshingTarget:self refreshingAction:@selector(getEatNetWork)];
//    [self.tableView.mj_header beginRfreshing];
    
    
}
#pragma mark ----- tableView协议方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    EatTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"eatTableViewIdentifer"];
    if (self.eatDataSource.count > indexPath.row) {
        cell.eatModel = self.eatDataSource[indexPath.row];
        [cell nightMode];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.eatDataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 205;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    EatShowViewController *showView = [[EatShowViewController alloc] init];
    EatModel *eatModel = [[EatModel alloc] init];
    eatModel = self.eatDataSource[indexPath.row];
    showView.urlKind = @"吃";
    showView.urlString = eatModel.url;
    showView.urlTitle = eatModel.title;
    showView.urlImage = eatModel.cover_image_url;
    NSLog(@"🐢🐢%@", showView.urlImage);
    [self.navigationController pushViewController:showView animated:YES];
}

#pragma mark ----- 吃货网络请求
-(void)getEatNetWork {
    [FSDNetWorkTool getWithURL:KEatGETURL withParameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
        
        NSDictionary *dictionary = [result valueForKey:@"data"];
        NSArray *array = [dictionary objectForKey:@"items"];
        self.eatDataSource = [[NSMutableArray alloc] initWithCapacity:0];
        for (NSDictionary *dic in array) {
            EatModel *eatModel = [[EatModel alloc] initWithDictionary:dic];
            [self.eatDataSource addObject:eatModel];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            self.HUD.hidden = YES;
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
        });
    } fail:^(NSError *error) {
        self.HUD.labelText = @"加载失败";
        [self.HUD hide:YES afterDelay:3];
    }];
}

#pragma mark - 加载数据
- (void)loadingData {
    NSInteger x  = ++_num * 10;
    NSString *string = [@"http://api.liwushuo.com/v2/channels/118/items?limit=20&offset=" stringByAppendingString:[NSString stringWithFormat:@"%ld", (long)x] ];
    NSString *stringUrl = [string stringByAppendingString:@"&gender=1"];
    
    [FSDNetWorkTool getWithURL:stringUrl withParameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
        
        NSDictionary *dictionary = [result valueForKey:@"data"];
        NSArray *array = [dictionary objectForKey:@"items"];
        for (NSDictionary *dic in array) {
            EatModel *eatModel = [[EatModel alloc] initWithDictionary:dic];
            [self.eatDataSource addObject:eatModel];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
            [self.tableView.mj_footer endRefreshing];
        });
    } fail:^(NSError *error) {
        
    }];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    [self.view nightMode];
    [self.tableView nightMode];
    
    [self.tableView reloadData];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
